package com.cz.nongyetongv5.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.baidu.mapapi.model.LatLng;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.activity.JianGuanDuiXiangActivity;
import com.cz.nongyetongv5.adapter.dao.DatabaseHelper;
import com.cz.nongyetongv5.bean.BeiJianDanWei;
import com.cz.nongyetongv5.bean.JianGuanDuiXiangFilter;
import com.cz.nongyetongv5.bean.Town;
import com.cz.nongyetongv5.utils.ConfigUtil;

import java.util.List;

public class JianGuanDuiXiangListAdapter extends BaseAdapter {

	// 上下文对象
	private Context context = null;

	private List datas = null;

	private DatabaseHelper db = null;

	private JianGuanDuiXiangActivity onClickListener = null;

	private JianGuanDuiXiangFilter filter = null;

	private AMapLocation location = null;

	private static final double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

	private ConfigUtil configUtil = null;

	public JianGuanDuiXiangListAdapter(Context context, List datas,
									   DatabaseHelper db, JianGuanDuiXiangActivity onClickListener,
									   JianGuanDuiXiangFilter filter, AMapLocation location) {
		this.context = context;
		this.datas = datas;
		this.db = db;
		this.onClickListener = onClickListener;
		this.filter = filter;

		this.location = location;

		configUtil = new ConfigUtil(context.getResources());

	}

	@Override
	public int getCount() {
		return datas == null ? 0 : datas.size();
	}

	@Override
	public Object getItem(int arg0) {
		return datas.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {

		ViewGroup tongXunLuItemLayout = null;

		if (view == null) {
			tongXunLuItemLayout = (ViewGroup) LayoutInflater.from(context)
					.inflate(R.layout.jianguanduixiang_item_layout, null);
		} else {
			tongXunLuItemLayout = (ViewGroup) view;
		}

		ViewGroup llTongXuLuRightLayout = (ViewGroup) tongXunLuItemLayout
				.findViewById(R.id.llTongXuLuRightLayout);

		View ivCall = tongXunLuItemLayout.findViewById(R.id.ivCall);

		View ivMore = tongXunLuItemLayout.findViewById(R.id.ivMore);

		TextView tvTitle = (TextView) tongXunLuItemLayout
				.findViewById(R.id.tvTitle);

		TextView tvJvLi = (TextView) tongXunLuItemLayout
				.findViewById(R.id.tvJvLi);

		Object data = datas.get(position);

		if (data != null && data instanceof BeiJianDanWei) {
			BeiJianDanWei beiJianDanWei = (BeiJianDanWei) data;

			tvTitle.setText(beiJianDanWei.getName());

			if (configUtil.gis()) {

				if (beiJianDanWei.getLatitude() > 0
						&& beiJianDanWei.getLongitude() > 0) {

					tvJvLi.setVisibility(View.VISIBLE);

					if (location != null) {

						double startLatiude = location.getLatitude();

						double startLongitude = location.getLongitude();

						String[] pointArr = bd_to_gd(
								beiJianDanWei.getLatitude(),
								beiJianDanWei.getLongitude()).split(",");

						double endLatiude = Double.parseDouble(pointArr[1]);

						double endLongitude = Double.parseDouble(pointArr[0]);

						LatLng startPoint = new LatLng(startLatiude,
								startLongitude);
						LatLng endPoint = new LatLng(endLatiude, endLongitude);

//						float length = AMapUtils.calculateLineDistance(
//								startPoint, endPoint);
//
//						String text = "";
//
//						if (length < 1000) {
//
//							text = "相距" + length + "米";
//
//						} else {
//
//							length = length / 1000;
//
//							DecimalFormat r = new DecimalFormat();
//
//							r.applyPattern("#0.00");// 保留小数位数，不足会补零
//
//							double result = new Double(r.format(length))
//									.doubleValue();
//
//							text = "相距" + result + "公里";
//
//						}
//
//						tvJvLi.setText(text);

					}

				} else {
					//tvJvLi.setText("未填写坐标信息");
					tvJvLi.setText("");
				}
			} else {
				tvJvLi.setVisibility(View.INVISIBLE);
			}

		}

		if (data != null && data instanceof Town) {

			tvJvLi.setVisibility(View.GONE);

			llTongXuLuRightLayout.setClickable(false);

			ivCall.setVisibility(View.GONE);
			ivMore.setVisibility(View.VISIBLE);

			Town town = (Town) data;

			int count =  db.queryCountBeiJianDanWeiByTownId(town.getCode(),this.filter);

			tvTitle.setText(town.getName() + "\u0020[" + count + "]");

		}

		return tongXunLuItemLayout;
	}

	public void setData(List datas) {
		this.datas = datas;
	}

	public void setFilter(JianGuanDuiXiangFilter filter) {
		this.filter = filter;
	}

	public void setLocation(AMapLocation location) {
		this.location = location;

		this.notifyDataSetChanged();
	}

	/**
	 * 百度地图的资源转高德
	 *
	 * @param bd_lat
	 * @param bd_lon
	 * @return
	 */
	private String bd_to_gd(double bd_lat, double bd_lon) {
		double x = bd_lon - 0.0065, y = bd_lat - 0.006;
		double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
		double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
		double gg_lon = z * Math.cos(theta);
		double gg_lat = z * Math.sin(theta);

		return gg_lon + "," + gg_lat;
	}

}
